package com.sq.partner.manager.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.mybatis.model.ActirationCardCancel;
import com.sq.partner.manager.mybatis.model.GatewayUser;
import com.sq.partner.manager.service.ActirationCardCancelService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.SqlUtil;
import com.sq.partner.manager.util.StrUtil;

/**
 * 激活卡作废
 * 
 * SQ
 *
 */
@Controller
@RequestMapping(value = "/cancelCard")
public class ActirationCardCancelController {

    @Resource
    private ActirationCardCancelService cancelService;

    /**
     * 激活卡作废页面
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/index")
    public String indexLoad() throws Exception {
        return "actirationCard/cancelCard/index";
    }

    /**
     * 激活卡作废统计页面
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/countCancel")
    public String countCancelCard() throws Exception {
        return "actirationCard/cancelCard/countCancel";
    }

    /**
     * 总库存查询 查询所有机构总库存（查询条件，卡模板，卡号）
     * 
     * @param session
     * @param cardDefineId
     *            卡定义模板ID
     * @param status
     *            卡单状态
     * @param visaType
     *            单证类型
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryStock", method = RequestMethod.POST)
    @ResponseBody
    public Page<ActirationCardCancel> queryStock(HttpSession session,
            Page<ActirationCardCancel> page, String cardDefineName,
            String startNo, String endNo) throws Exception {
        GatewayUser gatewayuser = (GatewayUser) session
                .getAttribute(Constants.SESSION_USER);
        // 初始化分页信息
        Map<String, Object> paramMap = SqlUtil.buildPageMap(page);
        // 添加查新条件
        if (StrUtil.isNotEmpty(cardDefineName)) {
            paramMap.put("cardDefineName", "%" + cardDefineName + "%");
        }
        if (StrUtil.isNotEmpty(startNo)) {
            paramMap.put("startNo", startNo);
        }
        if (StrUtil.isNotEmpty(endNo)) {
            paramMap.put("endNo", endNo);
        }
        if (!"00".equals(gatewayuser.getUserComcode())) {
            paramMap.put("deptNo", gatewayuser.getUserComcode());
        }
        paramMap.put("status", Constants.ACTIR_CARD_STOCK);
        page.setMap(paramMap);
        page = cancelService.queryStock(page);
        return page;
    }

    /**
     * 选中复选框作废
     * 
     * @param cardDefineIdList
     * @param cardNoList
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/cardCancelList", method = RequestMethod.POST)
    @ResponseBody
    public Result cardCancelList(@RequestParam List<String> cardDefineIdList,
            @RequestParam List<String> cardNoList) throws Exception {
        if (cardDefineIdList == null || cardDefineIdList.size() == 0
                || cardNoList == null || cardNoList.size() == 0
                || cardNoList.size() != cardDefineIdList.size()) {
            return Result.error("传参有误！");
        }
        return cancelService.cardCancelList(cardDefineIdList, cardNoList);
    }

    /**
     * 根据卡号区间作废
     * 
     * @param cardDefineId
     * @param startNo
     * @param endNo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/cardCancelBatch", method = RequestMethod.POST)
    @ResponseBody
    public Result cardCancelBatch(String cardDefineId, String startNo,
            String endNo) throws Exception {
        HashMap<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("cardDefineId", cardDefineId);
        paramMap.put("startNo", startNo);
        paramMap.put("endNo", endNo);
        return cancelService.cardCancelBatch(paramMap);
    }

    /**
     * 根据卡模板作废件数统计
     * 
     * @param cardDefineId
     * @param startNo
     * @param endNo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryCancel", method = RequestMethod.POST)
    @ResponseBody
    public Page<ActirationCardCancel> queryCancel(HttpSession session,
            Page<ActirationCardCancel> page, String cardDefineName)
            throws Exception {
        GatewayUser gatewayuser = (GatewayUser) session
                .getAttribute(Constants.SESSION_USER);
        // 初始化分页信息
        Map<String, Object> paramMap = SqlUtil.buildPageMap(page);
        // 添加查新条件
        if (StrUtil.isNotEmpty(cardDefineName)) {
            paramMap.put("cardDefineName", "%" + cardDefineName + "%");
        }
        if (!"00".equals(gatewayuser.getUserComcode())) {
            paramMap.put("deptNo", gatewayuser.getUserComcode());
        }
        paramMap.put("status", Constants.ACTIR_CARD_WASTE);
        page.setMap(paramMap);
        page = cancelService.queryCancel(page);
        return page;
    }

    /**
     * 根据卡模板作废明细
     * 
     * @param cardDefineId
     * @param startNo
     * @param endNo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryCancelDetail", method = RequestMethod.POST)
    @ResponseBody
    public Page<ActirationCardCancel> queryCancelDetail(HttpSession session,
            Page<ActirationCardCancel> page, String cardDefineId,
            String deptNo, String status) throws Exception {
        // 初始化分页信息
        Map<String, Object> paramMap = SqlUtil.buildPageMap(page);
        // 添加查新条件
        paramMap.put("cardDefineId", cardDefineId);
        paramMap.put("deptNo", deptNo);
        paramMap.put("status", status);
        page.setMap(paramMap);
        page = cancelService.queryCancelDetail(page);
        return page;
    }
}
